//
//  GYService.m
//  LockManagement
//
//  Created by apple on 2018/7/30.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYService.h"
#import "GYPeriphreal.h"
#import "GYCharacteristic.h"

@interface GYService()

/** 服务中所有的特征 */
@property(nonatomic, strong ,readwrite) NSMutableArray<GYCharacteristic *> *characteristicArray;

/** 发下特征的block回调数组 */
@property (nonatomic, copy) NSMutableArray<blueManagerSearchCharacteristicBlock> *findCharacterCallbackArray ;

/** 外围设备 */
@property (nonatomic, strong, readwrite) GYPeriphreal *periphreal;
@end

@implementation GYService

- (instancetype)initWithService:(CBService *)service {
    return [self initWithService:service perpheral:self.periphreal];
}

- (instancetype)initWithService:(CBService *)service perpheral:(GYPeriphreal *)peripheral {
    NSAssert(service, @"you should have a service to create easyservice !");
    
    if (self = [super init]) {
        self.periphreal = peripheral;
        self.service = service;
        self.isOn = YES;
        self.isEnabled = YES;
    }
    
    return self;
}

- (NSString *)name {
    return [NSString stringWithFormat:@"%@",self.service.UUID];
}

- (CBUUID *)UUID {
    return self.service.UUID;
}

- (NSArray *)includedServices
{
    return self.service.includedServices ;
}

- (void)discoverCharacteristicWithCallBack:(blueManagerSearchCharacteristicBlock)callBack {
    [self discoverCharacteristicWithCharacteristicUUIDs:nil callback:callBack];
}

- (void)discoverCharacteristicWithCharacteristicUUIDs:(NSArray<CBUUID *> *)uuidArray callback:(blueManagerSearchCharacteristicBlock)callback {
    
    NSAssert(callback, @"you should deal the callback");
    
    if (callback) {
        [self.findCharacterCallbackArray addObject:callback];
    }
    
    BOOL isAllUUIDExited = uuidArray.count > 0 ;//需要查找的UUID是否都存在
    for (CBUUID *tempUUID in uuidArray) {
        
        BOOL isExitedUUID = NO ;//数组里单个需要查找到UUID是否存在
        for (GYCharacteristic *tempCharacter in self.characteristicArray) {
            if ([tempCharacter.UUID isEqual:tempUUID]) {
                isExitedUUID = YES ;
                break ;
            }
        }
        if (!isExitedUUID) {
            isAllUUIDExited = NO ;
            break ;
        }
    }
    
    if (isAllUUIDExited) {
        
        if (self.findCharacterCallbackArray.count > 0) {
            blueManagerSearchCharacteristicBlock callback = self.findCharacterCallbackArray.firstObject ;
            callback(self.characteristicArray,nil);
            callback = nil ;
            
            [self.findCharacterCallbackArray removeObjectAtIndex:0];
        }
        
    }else{
        
        NSLog(@"寻找设备服务上的特征 %@  %@",self.periphreal.identifier.UUIDString,self.service.UUID);
        
        //发现特征
        [self.periphreal.periphreal discoverCharacteristics:uuidArray forService:self.service];
    }
}

- (void)dealDiscoverCharacteristic:(NSArray *)characteristics error:(NSError *)error {
    
    for (CBCharacteristic *tempCharacteristic in characteristics) {
        
        GYCharacteristic *tempC  = [self searchCharacteristciWithCharacteristic:tempCharacteristic] ;
        if (nil == tempC) {
            GYCharacteristic *character = [[GYCharacteristic alloc]initWithCharacteristic:tempCharacteristic perpheral:self.periphreal];
            [self.characteristicArray addObject:character];
        }
    }
    
    if (self.findCharacterCallbackArray.count > 0) {
        blueManagerSearchCharacteristicBlock callback = self.findCharacterCallbackArray.firstObject ;
        callback(self.characteristicArray,nil);
        callback = nil ;
        
        [self.findCharacterCallbackArray removeObjectAtIndex:0];
    }
}

- (GYCharacteristic *)searchCharacteristciWithCharacteristic:(CBCharacteristic *)characteristic {
    GYCharacteristic *tempC = nil ;
    for (GYCharacteristic *tCharacterstic in self.characteristicArray) {
        if ([characteristic.UUID isEqual:tCharacterstic.UUID]) {
            tempC = tCharacterstic ;
            break ;
        }
    }
    return tempC ;
}



#pragma mark -- 懒加载

- (NSMutableArray *)characteristicArray
{
    if (nil == _characteristicArray) {
        _characteristicArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _characteristicArray ;
}


- (NSMutableArray<blueManagerSearchCharacteristicBlock> *)findCharacterCallbackArray {
    if (!_findCharacterCallbackArray) {
        _findCharacterCallbackArray = [NSMutableArray array];
    }
    
    return _findCharacterCallbackArray;
}













@end
